{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "from PIL import Image\n",
    "import cv2\n",
    "\n",
    "# 先创建下面的文件夹，把icdar2017rctw_train_v1.2/train/下的所有图片复制到target_img_dir文件夹下\n",
    "base_dir = \"ICDAR2017/VOC2007\"\n",
    "\n",
    "target_img_dir = base_dir + \"/\" + \"JPEGImages/\"\n",
    "target_ann_dir = base_dir + \"/\" + \"Annotations/\"\n",
    "target_set_dir = base_dir + \"/\" + \"ImageSets/\"\n",
    "\n",
    "# 把该路径下的所有.txt文件转转成xml存到target_ann_dir文件夹下\n",
    "train_txt_dir = \"icdar2017rctw_train_v1.2/train/\"\n",
    "\n",
    "img_list = []\n",
    "\n",
    "for file_name in os.listdir(target_img_dir):\n",
    "    if file_name.split('.')[-1] == 'jpg':\n",
    "        img_list.append(file_name)\n",
    "\n",
    "for idx in range(len(img_list)):\n",
    "    img_name = target_img_dir + img_list[idx]\n",
    "    gt_name = train_txt_dir + 'image_' + img_list[idx].split('.')[0].split('_')[1] + '.txt'\n",
    "    print(img_list)\n",
    "\n",
    "    gt_obj = open(gt_name, 'r')\n",
    "\n",
    "    gt_txt = gt_obj.read()\n",
    "\n",
    "    gt_split = gt_txt.split('\\n')\n",
    "\n",
    "    img = cv2.imread(img_name)\n",
    "\n",
    "    im = Image.open(img_name)\n",
    "    imgwidth, imgheight = im.size\n",
    "\n",
    "    # write in xml file\n",
    "    xml_file = open(\n",
    "        (target_ann_dir + img_list[idx].split('.')[0] + '.xml'), 'w')\n",
    "    xml_file.write('<annotation>\\n')\n",
    "    xml_file.write('    <folder>text</folder>\\n')\n",
    "    xml_file.write('    <filename>' + img_list[idx] + '</filename>\\n')\n",
    "    xml_file.write('    <source>\\n')\n",
    "    xml_file.write('        <database>icdar2017</database>\\n')\n",
    "    xml_file.write('        <annotation>text</annotation>\\n')\n",
    "    xml_file.write('        <flickrid>000000</flickrid>\\n')\n",
    "    xml_file.write('    </source>\\n')\n",
    "    xml_file.write('    <owner>\\n')\n",
    "    xml_file.write('        <width>zhl</width>\\n')\n",
    "    xml_file.write('    </owner>\\n')\n",
    "    xml_file.write('    <size>\\n')\n",
    "    xml_file.write('        <width>' + str(imgwidth) + '</width>\\n')\n",
    "    xml_file.write('        <height>' + str(imgheight) + '</height>\\n')\n",
    "    xml_file.write('        <depth>3</depth>\\n')\n",
    "    xml_file.write('    </size>\\n')\n",
    "\n",
    "    f = False\n",
    "    for gt_line in open(gt_name):\n",
    "        gt_ind = gt_line.split(',')\n",
    "        xmin = min(gt_ind[0], gt_ind[6])\n",
    "        ymin = min(gt_ind[1], gt_ind[3])\n",
    "        xmax = max(gt_ind[2], gt_ind[4])\n",
    "        ymax = max(gt_ind[5], gt_ind[7])\n",
    "\n",
    "        xml_file.write('    <object>\\n')\n",
    "        xml_file.write('        <name>text</name>\\n')\n",
    "        xml_file.write('        <pose>none</pose>\\n')\n",
    "        xml_file.write('        <truncated>0</truncated>\\n')\n",
    "        xml_file.write('        <difficult>' + str(gt_ind[8]) + '</difficult>\\n')\n",
    "        xml_file.write('        <bndbox>\\n')\n",
    "        xml_file.write('            <xmin>' + str(xmin) + '</xmin>\\n')\n",
    "        xml_file.write('            <ymin>' + str(ymin) + '</ymin>\\n')\n",
    "        xml_file.write('            <xmax>' + str(xmax) + '</xmax>\\n')\n",
    "        xml_file.write('            <ymax>' + str(ymax) + '</ymax>\\n')\n",
    "        xml_file.write('        </bndbox>\\n')\n",
    "        xml_file.write('    </object>\\n')\n",
    "\n",
    "    xml_file.write('</annotation>')\n",
    "\n",
    "img_lists = glob.glob(target_ann_dir + '/*.xml')\n",
    "img_names = []\n",
    "for item in img_lists:\n",
    "    temp1, temp2 = os.path.splitext(os.path.basename(item))\n",
    "    img_names.append(temp1)\n",
    "\n",
    "train_fd = open(target_set_dir + \"/Main/trainval.txt\", 'w')\n",
    "for item in img_names:\n",
    "    train_fd.write(str(item) + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-03T09:38:35.750874Z",
     "start_time": "2020-03-03T09:38:35.735914Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['205', '158', '215', '160', '220', '229', '215', '219', '0', '\"好的\"']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gt_line = '205,158,215,160,220,229,215,219,0,\"好的\"'\n",
    "gt_ind = gt_line.split(',')\n",
    "gt_ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-03T09:41:20.815418Z",
     "start_time": "2020-03-03T09:41:20.808436Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('205', '158', '220', '229')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xmin = min(gt_ind[0], gt_ind[6])\n",
    "ymin = min(gt_ind[1], gt_ind[3])\n",
    "xmax = max(gt_ind[2], gt_ind[4])\n",
    "ymax = max(gt_ind[5], gt_ind[7])\n",
    "xmin,ymin,xmax,ymax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
